<?php

namespace App\Http\Controllers\Wechat\User;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repos\OrderRepo;
use App\Http\Requests\Api\OrderRequest;

class OrderController extends Controller
{
    public function index(OrderRequest $request)
    {
    	$rs['status'] = false;
    	$where['user_id'] = $request->user_id;
    	if($request->status) {
    		$where['status'] = $request->status;
    	}
    	$list = OrderRepo::pages($where);
    	if($list) {
    		$rs['status'] = true;
    		$rs['data']   = $list;
    		return response()->json($rs);
    	}
    	return response()->json($rs);
    }

    public function show(OrderRequest $request)
    {
    	$rs['status'] = false;
    	$where['id'] = $request->order_id;
    
    	$data = OrderRepo::find($where);
    	if($data) {
    		$rs['status'] = true;
            $unifyData['openid']      = object_get($data,'user.openid');
            $unifyData['total_money'] = $data->pre_money;
            $unifyData['code']        = $data->code;
            $unifyData['order_id']    = $data->id;  
                  
            $rs['data']['pay'] = OrderRepo::wechatUnifyOrder($unifyData);
    		$rs['data'] = $data;

    		return response()->json($rs);
    	}
    	return response()->json($rs);
    }

    public function orderCount(OrderRequest $request)
    {
        $rs['status'] = true;
        $where['user_id'] = $request->user_id;

        $where['status']  = ['1'];
        $where['key']     = 'wait_pay';
        $num['wait_pay']  = OrderRepo::count($where);  

        $where['status'] = ['3','4'];
        $where['key']    = 'wait_receive';
        $num['wait_receive'] = OrderRepo::count($where);;


        $where['status'] = ['100'];
        $where['key']    = 'wait_point';
        $num['wait_point']   = OrderRepo::count($where);

        $rs['data'] = $num;
        return response()->json($rs)->setEncodingOptions(JSON_NUMERIC_CHECK);
    }

    

    public function cancle(OrderRequest $request)
    {
        $validatedData = $request->validate([
            'order_code' => 'required',        
        ]);

        $rs['status']   = true;
        $data['code']  = $request->order_code;
        
        $data['remark'] = $request->remark;
        $flag = OrderRepo::cancle($data);
        if($flag['status']==false) {
            $rs['msg'] = $flag['msg'];
            return response()->json($rs);
        }
        return response()->json($rs);

    }

    /**
     * 支付预付款
     */
    public function prepay(OrderRequest $request)
    {
        $rs['status']        = false;
        $data['openid']      = $request->user->openid;
        $data['order_id']          = $request->order_id;
      
        $flag = OrderRepo::prepay($data);
        if($flag['status'] == true) {
            $rs['data'] = $flag['data'];
            return response()->json($rs);
        }
        $rs['msg'] = $flag['msg'];
        return response()->json($rs);
    }

   

    /**
     * 支付尾款
     */
    public function pay(OrderRequest $request)
    {
        $rs['status']      = false;
        $data['openid']    = $request->user->openid;
        $data['order_id']  = $request->order_id;
       
        $flag = OrderRepo::pay($data);
        if($flag['status'] == true) {
            $rs['data'] = $flag['data'];
            return response()->json($rs);
        }
        $rs['msg'] = $flag['msg'];
        return response()->json($rs);
    }




    /**
     * 评价订单
     */
    public function point(OrderRequest $request)
    {
        $validatedData = $request->validate([
            'order_code' => 'required',    
            'product_id' => 'required', 
            'point'      => 'required',   
        ]);

        $rs['status']       = true;
        $data = $request->pure();
      
        $data['user_id']    = $request->user_id;
        $data['code']       = array_pull($data,'order_code');
      
        $flag = OrderRepo::point($data);
        if($flag['status']==false) {
            $rs['status'] = false;
            $rs['msg'] = $flag['msg'];
            return response()->json($rs);
        }
        return response()->json($rs);

    }
   

    
}
